package edu.thu.studio.ormdesigner.editor.command;

import edu.thu.studio.ormdesigner.common.AbstractCommand;
import edu.thu.studio.ormdesigner.model.diagram.Diagram;
import edu.thu.studio.ormdesigner.model.diagram.node.ViewNode;
import edu.thu.studio.ormdesigner.model.orm.view.View;
import edu.thu.studio.ormdesigner.util.Utils;

public class CreateViewNodeCommand extends AbstractCommand {
    
    private Diagram diagram;
    
    private View view;
    
    private ViewNode node;
    
    public CreateViewNodeCommand(Diagram diagram, ViewNode node) {
        this.diagram = diagram;
        this.view = new View();
        view.checkAndInit();
        
        node.setRefId(view.getId());
        node.setDiagram(diagram);
        node.setRefTable(view);
        node.checkAndInit();
        this.node = node;
    }
    
    public CreateViewNodeCommand(Diagram diagram, ViewNode node, View view) {
        this.diagram = diagram;
        this.view = view;
        view.checkAndInit();
        
        node.setRefId(view.getId());
        node.setDiagram(diagram);
        node.setRefTable(view);
        node.checkAndInit();
        this.node = node;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void doExecute() {
        Utils.getViewSet().addObject(view);
        this.diagram.getNodes().addObject(node);
        this.diagram.refreshChildren();
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void doUndo() {
        Utils.getViewSet().remove(view);
        this.diagram.getNodes().remove(node);
        this.diagram.refreshChildren();
    }
    
}
